package com.rain.cy.business.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.rain.cy.business.model.dto.CountDataDTO;
import com.rain.cy.business.model.dto.CountDataPieDTO;
import com.rain.cy.business.model.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yu_chen
 * @since 2021/10/7 22:20
 */
@Service
public class PatientCountService {


    @Autowired
    private UserScoreService userScoreService;


    @Autowired
    private ActionCountService actionCountService;

    /**
     * 药品存量统计
     *
     * @param userId 医生ID
     * @return 月度统计
     */
    public CountDataDTO getScoreCount(Integer userId) {
        //可以增加用户ID过滤
        List<UserScoreEntity> userScoreEntities = userScoreService.lambdaQuery().eq(UserScoreEntity::getUserId, userId).orderByAsc(UserScoreEntity::getCreateDay).list();
        List<String> createDays = userScoreEntities.stream()
                .map(UserScoreEntity::getCreateDay)
                .collect(Collectors.toList());
        List<String> legendList = Arrays.asList("治疗进展", "康复进展");
        CountDataDTO countDataDTO = CountDataDTO.of().legendList(legendList).xTypeStrList(createDays);
        if (CollectionUtil.isEmpty(userScoreEntities)) {
            return countDataDTO;
        }
        for (int i = 0; i < legendList.size(); i++) {
            for (UserScoreEntity userScoreEntity : userScoreEntities) {
                int score = 0;
                if (i == 0) {
                    score = userScoreEntity.getScoreSub1();
                } else if (i == 1) {
                    score = userScoreEntity.getScoreSub2();
                }
                countDataDTO.getSeriesList().get(i).getData().add(score);
            }
        }
        return countDataDTO;
    }


    public CountDataPieDTO getActionCount(Integer userId) {
        ActionCountEntity actionCountEntity = actionCountService.lambdaQuery().eq(ActionCountEntity::getUserId, userId).oneOpt().orElse(new ActionCountEntity());
        CountDataPieDTO countDataPieDTO = new CountDataPieDTO();
        countDataPieDTO.append("收到反馈次数", ObjectUtil.defaultIfNull(actionCountEntity.getReceiveFeedbackNum(),0));
        countDataPieDTO.append("分享次数", ObjectUtil.defaultIfNull(actionCountEntity.getShareNum(),0));
        countDataPieDTO.append("发送反馈次数", ObjectUtil.defaultIfNull(actionCountEntity.getSendFeedbackNum(),0));
        countDataPieDTO.append("评分超过4.5星的次数", ObjectUtil.defaultIfNull(actionCountEntity.getScoreMoreNum(),0));
        return countDataPieDTO;
    }

}
